package com.location.footprint.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.location.footprint.R;
import com.location.footprint.base.BaseAppAplication;
import com.location.footprint.bean.CommentsBean;
import com.location.footprint.bean.CommentsTypeBean;
import com.location.footprint.utils.GlideUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


public class CommentsAdapter extends BaseMultiItemQuickAdapter<CommentsBean, BaseViewHolder> {

    public CommentsAdapter(List data) {
        super(data);
//        Collections.reverse(data);
        addItemType(CommentsBean.COMMENTS_PARENT, R.layout.item_comments);
        addItemType(CommentsBean.COMMENTS_LIKES, R.layout.item_like_list);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentsBean item) {
        switch (helper.getItemViewType()) {
            case CommentsBean.COMMENTS_PARENT:
                CircleImageView img_avatar = helper.getView(R.id.img_avatar);
                GlideUtils.loadImageView(mContext, item.getAvatarPath(), img_avatar);
                TextView text_content = helper.getView(R.id.text_comment);
                text_content.setText(item.getContent());
                TextView text_name = helper.getView(R.id.text_name);
                text_name.setTextColor(ContextCompat.getColor(mContext, BaseAppAplication.theme));
                text_name.setText(item.getNickName());
                helper.addOnClickListener(R.id.text_like);
                helper.addOnClickListener(R.id.text_comments);
                RecyclerView recyclerView = helper.getView(R.id.recyclerview_child);
                recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                recyclerView.setHasFixedSize(true);
                CommentsChildAdapter hAdapter = new CommentsChildAdapter();
                recyclerView.setAdapter(hAdapter);
                hAdapter.addData(item.getChildBeanList());
                if (item.getCommentsCount() > 5) {
                    TextView textView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_child_comments, null);
                    textView.setText("查看更多回复(" + item.getCommentsCount() + ")");
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Integer integer = new Integer(helper.getLayoutPosition());
                            EventBus.getDefault().post(integer);
                        }
                    });
                    hAdapter.setFooterView(textView);
                }
                recyclerView.setNestedScrollingEnabled(false);
                hAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        CommentsTypeBean bean = new CommentsTypeBean(CommentsTypeBean.COMMENTS_REPLY_CHILD);
                        bean.setPosition(helper.getLayoutPosition());
                        bean.setPosition2(position);
                        EventBus.getDefault().post(bean);
                    }
                });

                break;
            case CommentsBean.COMMENTS_LIKES:
                CircleImageView avatar = helper.getView(R.id.img_avatar);
                GlideUtils.loadImageView(mContext, item.getAvatarPath(), avatar);
                TextView name = helper.getView(R.id.text_title);
                name.setText(item.getNickName());

                break;

        }
    }

}
